local sk__muyi = fk.CreateSkill {

  name = "sk__muyi",

  tags = {},

}



sk__muyi:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target ~= player and target.phase == Player.Play and not target.dead then
      return player:hasSkill(sk__muyi.name) and not target.dead and not target:isKongcheng()
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__muyi.name, nil, "#sk__muyi-invoke::" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    local n = math.min(#target:getCardIds("he"), 2)
    local cards = room:askForCard(target, n, n, false, sk__muyi.name, false, ".", "#sk__muyi-give::" ..
      player.id .. ":" .. n)
    room:setPlayerMark(player, "sk__muyi-phase", n)
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, sk__muyi.name, nil, false, target)
  end,
})
sk__muyi:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target ~= player and target.phase == Player.Play and not target.dead then
      return player:usedSkillTimes(sk__muyi.name, Player.HistoryPhase) > 0 and not player:isNude()
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    local n = math.min(#player:getCardIds("he"), player:getMark("sk__muyi-phase"))
      local cards = room:askForCard(player, n, n, true, sk__muyi.name, false, ".", "#sk__muyi-give::" ..
        target.id .. ":" .. n)
      room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonGive, sk__muyi.name, nil, false, player)
  end,
})

return sk__muyi
